package cn.qqncn.furniture.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.GenericResult;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ProcessObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Formatter;
public class OSSTool {

    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI4G137pXrMnoLbebD4uPG";
    private static String accessSecret = "poB2A03mTpWPTNEOEIVYzfBA0kQ3VE";
    private static String bucketName = "zkw-furniture";
    private static String sufferOutUrl = "http://zkw-furniture.oss-cn-hangzhou.aliyuncs.com/";



    /**
     * 上传文件
     * @param fileId id
     * @param file 文件
     * @return 文件路径
     */
     public static String uploadFile(String fileId, MultipartFile file) {
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = fileId + ext;
        String objName =   "img/" + fileName;

        if (upload(objName, file)){
            String url = sufferOutUrl + objName;
            return url;
         }else {
            return null;
        }

    }





    /**
     * 上传到OSS服务器
     * @param objName 对象名   日期/文件id.扩展名
     * @param file 文件
     * @return 上传状态
     */
    private static Boolean upload(String objName, MultipartFile file) {

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);

        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(file.getBytes());
            ossClient.putObject(bucketName, objName, inputStream);
            ossClient.shutdown();
            return true;
        } catch (Exception e) {
            System.out.println("上传失败");
            ossClient.shutdown();
            return false;
        }
    }


    /**
     * 删除OSS文件服务器上的文件
     * @param filePath 文件位置
     */
    public static void delete(String filePath) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);
        if (ossClient.doesObjectExist(bucketName, filePath)) {
            System.out.println("文件存在");
            ossClient.deleteObject(bucketName, filePath);
            System.out.println("删除成功");
        }else {
            System.out.println("文件不存在");
        }
        ossClient.shutdown();
    }


    /**
     * 上传头像
     * @param userId 用户id
     * @param imgFile 图片文件
     * @return 头像路径
     */
    public static String uploadPortrait(String userId, MultipartFile imgFile){

        String ext = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        String fileName = userId + ext;
        String objName = "portrait-cache/" + fileName;
        upload(objName, imgFile);
        String url = sufferOutUrl + "/" + objName;
        return  url;
    }




}
